package com.meowcook.controller;

import com.meowcook.common.AppResult;
import com.meowcook.config.AppConfig;
import com.meowcook.pojo.dto.MessageDTO;
import com.meowcook.pojo.vo.MessageVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-11-15
 * Time: 14:03
 */
@Slf4j
@Api(tags = "消息操作接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    public MessageVO createMessage(@Valid @RequestBody MessageDTO messageDTO) {
        return messageService.createMessage(messageDTO);
    }

    @ApiOperation("获取未读消息数")
    @GetMapping("/getUncheckNum")
    public AppResult<Integer> getUncheckNum(HttpServletRequest  request) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        return AppResult.success(messageService.getUncheckNum(user.getId()));
    }

    public AppResult<ArrayList<Long>> getMessageIdList(HttpServletRequest request,
                                                 @ApiParam("会话id") @RequestParam("sessionId") @NonNull Long sessionId) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        return AppResult.success(messageService.getMessageIdList(user.getId(), sessionId));
    }

    @ApiOperation("获取会话消息列表")
    @PostMapping("/getMessageList")
    public AppResult<ArrayList<MessageVO>> getMessageList(@ApiParam("消息Id列表") @NonNull @RequestBody ArrayList<Long> messageIdList) {
        log.info("消息Id列表: {}", messageIdList);
        return AppResult.success(messageService.getMessageList(messageIdList));
    }

    @ApiOperation("更新未读消息数")
    @PostMapping("/setUncheckNumZero")
    public AppResult setUncheckNumZero(HttpServletRequest request,
                                       @ApiParam("会话id") @RequestParam("sessionId") Long sessionId) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        messageService.updateUncheckNum(user.getId(), sessionId, 0);

        return AppResult.success("修改成功");
    }

    @ApiOperation("删除消息")
    @PostMapping("/deleteMessage")
    public void deleteMessage(HttpServletRequest request,
                              @ApiParam("消息Id列表") @NonNull ArrayList<Long> messageIdList,
                              @ApiParam("会话id") @RequestParam("sessionId") Long sessionId) {
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        messageService.deleteMessage(user.getId(),sessionId, messageIdList);
    }
}
